package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import mission4propre.AVLTree;
import mission4propre.ConstructeurArbre;
import mission4propre.LivreRef;

public class AnalyseActionListener implements ActionListener {
	
	private JTextField expression;
	private JTextArea expressionDerive;
	private ConstructeurArbre cons;
	private JComboBox box;

	public AnalyseActionListener(JTextField expression2, JTextArea expressionDerive,JComboBox box, ConstructeurArbre arbreCons) {
		this.expression=expression2;
		this.expressionDerive=expressionDerive;
		this.cons=arbreCons;
		this.box=box;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void actionPerformed(ActionEvent e) {
		if(expression.getText().matches("")){
			expressionDerive.setText("Entrez au moins un titre de revue");
		}
		else{
			//expressionDerive.setText(dico.getAuteurtoString(dico.getLivreRefByRevue(expression.getText())));
			LinkedList<Comparable> livre=null;;
			if(box.getSelectedItem().equals("titre")){
				LivreRef.Field=1;
				livre= searchByTitle(expression.getText());
			}
			if(box.getSelectedItem()=="num"){
				LivreRef.Field=2;
				livre= searchByNum(expression.getText());
			}
			if(box.getSelectedItem()=="rang"){
				LivreRef.Field=3;
				livre= searchByRang(expression.getText());
			}
			if(box.getSelectedItem()=="ROF1name"){
				LivreRef.Field=4;
				livre= searchByROF1name(expression.getText());
			}
			if(box.getSelectedItem()=="ROF2"){
				LivreRef.Field=5;
				livre= searchByROF2(expression.getText());
			}
			if(box.getSelectedItem()=="ROF2name"){
				LivreRef.Field=6;
				livre= searchByROF2name(expression.getText());
			}
			if(box.getSelectedItem()=="ROF3"){
				LivreRef.Field=7;
				livre= searchByROF3(expression.getText());
			}
			if(box.getSelectedItem()=="ROF3name"){
				LivreRef.Field=8;
				livre= searchByROF3name(expression.getText());
			}
			
			
			if(livre!=null){
				expressionDerive.setText("");
				Iterator<Comparable> ite= livre.iterator();
				while(ite.hasNext()){
					expressionDerive.setText(expressionDerive.getText()+ite.next().toString()+"\n"+"\n");
					//System.out.println(livre.size());
				}

			}
				else 
				expressionDerive.setText("le livre n'existe pas");
		}
	}
	
	
	@SuppressWarnings({ "rawtypes" })
	private LinkedList<Comparable> searchByROF3name(String text) {
		AVLTree avl = cons.generate();
		LivreRef livre =new LivreRef();
		livre.setROF3name(text);
		return (LinkedList<Comparable>) avl.findAll(livre);
	}

	@SuppressWarnings("rawtypes")
	private LinkedList<Comparable> searchByROF3(String text) {
		AVLTree avl = cons.generate();
		LivreRef livre =new LivreRef();
		livre.setROF3(text);
		return (LinkedList<Comparable>) avl.findAll(livre);
	}

	@SuppressWarnings("rawtypes")
	private LinkedList<Comparable> searchByROF2name(String text) {
		AVLTree avl = cons.generate();
		LivreRef livre =new LivreRef();
		livre.setROF2name(text);
		return (LinkedList<Comparable>) avl.findAll(livre);
	}

	@SuppressWarnings("rawtypes")
	private LinkedList<Comparable> searchByROF2(String text) {
		AVLTree avl = cons.generate();
		LivreRef livre =new LivreRef();
		livre.setROF2(text);
		return (LinkedList<Comparable>) avl.findAll(livre);
	}

	@SuppressWarnings("rawtypes")
	private LinkedList<Comparable> searchByROF1name(String text) {
		AVLTree avl = cons.generate();
		LivreRef livre =new LivreRef();
		livre.setROF1name(text);
		return (LinkedList<Comparable>) avl.findAll(livre);
	}

	@SuppressWarnings("rawtypes")
	private LinkedList<Comparable> searchByNum(String num) {
		AVLTree avl = cons.generate();
		LivreRef livre =new LivreRef();
		livre.setNum(num);
		return (LinkedList<Comparable>) avl.findAll(livre);
	}

	@SuppressWarnings("rawtypes")
	public LinkedList<Comparable> searchByTitle(String title){
		AVLTree avl = cons.generate();
		LivreRef livre =new LivreRef();
		livre.setTitre(title);
		return (LinkedList<Comparable>) avl.findAll(livre);
	}
	@SuppressWarnings("rawtypes")
	public LinkedList<Comparable> searchByRang(String title){
		AVLTree avl = cons.generate();
		LivreRef livre =new LivreRef();
		livre.setRang(title);
		return (LinkedList<Comparable>) avl.findAll(livre);
	}
	
	

}
